package com.example.myapplication.ui.person;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import com.example.myapplication.R;
import com.example.myapplication.model.TUser;
import com.example.myapplication.until.Adapter_User;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class PersonFragment extends Fragment {
    private PersonViewModel personViewModel;
    private ListView mListView;
    private List<TUser> mList = new ArrayList<>();
    int id=1;

    //访问本地接口，用10.0.2.2取代localhost不可以
    private String url = "http://192.168.1.4:8080/DoctorSystem/user/queryUserById?limit=10&page=1";



    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        personViewModel =
                ViewModelProviders.of(this).get(PersonViewModel.class);
        View root = inflater.inflate(R.layout.fragment_person, container, false);
       // final TextView textView = root.findViewById(R.id.text_person);
        personViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
         //       textView.setText(s);
            }
        });
        return root;


    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mListView = (ListView) getActivity().findViewById(R.id.mListView);
    }

    public void onWindowFocusChanged(boolean hasFocus)
    {
        /**
         * 请求数据
         * 解析json
         * 适配器
         * 绑值显示
         */
        if (hasFocus){

            RxVolley.get(url, new HttpCallback() {
                @Override
                public void onSuccess(String t) {
                 //   Toast.makeText(PersonFragment.this,t,Toast.LENGTH_LONG).show();
                    Toast.makeText(getActivity(), "个人中心", Toast.LENGTH_LONG).show();
                    //控制台输出Json
                    Log.d("aaaa",""+t);

                    //解析json
                    parsingJson(t);

                    Adapter_User adapter = new Adapter_User(getActivity(),mList);

                    mListView.setAdapter(adapter);


                }
            });
        }
    }
    //数据解析
    public void parsingJson(String t){
        try {

            JSONObject jsonObject = new JSONObject(t);
            JSONArray jsonList = jsonObject.getJSONArray("data");
            for(int i=0;i<jsonList.length();i++){
                JSONObject json = jsonList.getJSONObject(i);
                int userid=Integer.parseInt(json.getString("userid"));
                String name=json.getString("name");
                String phone=json.getString("phone");
                String gender=json.getString("gender");
                String classid=json.getString("classid");


                mList.add(new TUser(userid,name,phone,gender,classid));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

}